public class Chainbinarytree {
    public static void main(String[] args) {
        Herotree root=new Herotree(1,"宋江");
        Herotree node2=new Herotree(3,"卢俊义");
        Herotree node3=new Herotree(6,"林冲");
        Herotree node4=new Herotree(8,"李逵");
        Herotree node5=new Herotree(10,"tom");
        Herotree node6=new Herotree(14,"jack");
        root.setLeft(node2);
        root.setRight(node3);
        node2.setRight(node5);
        node2.setLeft(node4);
        node3.setLeft(node6);

        Chaintree chaintree = new Chaintree(root);
        chaintree.Chain();
        //System.out.println(node2.getRight());
        System.out.println();


        chaintree.HeroList();
    }
}

class Chaintree{
    private Herotree herotree;
    private Herotree pre;
    public Chaintree(Herotree herotree) {
        this.herotree = herotree;
    }
    public void Chain(){
        this.Chained(herotree);
    }
    public void Chained(Herotree herotree){//线索化

        if (herotree==null){
            return;
        }
        Chained(herotree.getLeft());
        if (herotree.getLeft()==null){//找到最左边的数据
            herotree.setLeft(pre);
            herotree.setLeftType(1);
        }
        if (pre!=null&&pre.getRight()==null){
            pre.setRight(herotree);
            pre.setRightType(1);
        }
        pre=herotree;//向后移
        Chained(herotree.getRight());
        System.out.println(herotree);
    }
    public void HeroList(){
        Herotree node=herotree;
        while (node!=null){
            while (node.getLeftType()==0){
                node= node.getLeft();
            }
            System.out.println(node);
            while (node.getRightType()==1){//状态类型为1表示可以进行操作
                node=node.getRight();
                System.out.println(node);
            }
            node=node.getRight();//结点向右移
        }

    }
}
class Herotree{
    private int no;
    private String name;
    private Herotree left;
    private Herotree right;
    private int leftType;
    private int rightType;
    private Herotree pre=null;

    public int getLeftType() {
        return leftType;
    }

    public void setLeftType(int leftType) {
        this.leftType = leftType;
    }

    public int getRightType() {
        return rightType;
    }

    public void setRightType(int rightType) {
        this.rightType = rightType;
    }

    public Herotree(int no, String name) {
        this.no = no;
        this.name = name;
    }

    public int getNo() {
        return no;
    }

    public String getName() {
        return name;
    }

    public Herotree getLeft() {
        return left;
    }

    public void setLeft(Herotree left) {
        this.left = left;
    }

    public Herotree getRight() {
        return right;
    }

    public void setRight(Herotree right) {
        this.right = right;
    }

    public void setNo(int no) {
        this.no = no;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "Hero{" +
                "no=" + no +
                ", name='" + name + '\'' +
                '}';
    }

}